/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JpanelOferiment.java
 *
 * Created on 19-may-2010, 17:51:04
 */

package edu.ieslaferreria.creditsintesis.builders.Swing;

import edu.ieslaferreria.creditsintesis.rmi.EventChangePanel;
import edu.ieslaferreria.creditsintesis.rmi.GameEventInteracctionTwoPanels;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
/**
 *
 * @author Administrador
 */
public class JpanelOferiment_1 extends JPanelGame{


  
    /** Creates new form JpanelOferiment */
    public JpanelOferiment_1() {
        initComponents();
        jTextArea1.setEditable(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBoxKindOffers = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButtonEnviar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButtonChat = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jButtonVolver = new javax.swing.JButton();

        jComboBoxKindOffers.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Chat"));

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
        );

        jButtonEnviar.setText("Enviar");
        jButtonEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEnviarActionPerformed(evt);
            }
        });

        jLabel1.setText("Tipus de Oferiments");

        jButtonChat.setText("Comunicar");
        jButtonChat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonChatActionPerformed(evt);
            }
        });

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButtonVolver.setText("Volver");
        jButtonVolver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVolverActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBoxKindOffers, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(109, 109, 109)
                        .addComponent(jButtonVolver, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButtonEnviar))
                .addGap(86, 86, 86)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonChat)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
                            .addGap(10, 10, 10))
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxKindOffers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButtonEnviar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 98, Short.MAX_VALUE)
                .addComponent(jButtonVolver))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonChat)
                .addGap(6, 6, 6))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonChatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonChatActionPerformed
       HashMap<String,List<String>>res= new HashMap();

       String resString = getClient().getNom() + ": " + jTextField1.getText();
       ArrayList<String> resArray =  new ArrayList();
      
//       jlist????? rellenar???? recoger?????
       resArray.add(resString);
       res.put(CHAT,resArray);
      

        try {
            this.getPontPanel().send(new GameEventInteracctionTwoPanels(-1,resArray, res, this.getPontPanel()));
        } catch (RemoteException ex) {
            Logger.getLogger(JpanelOferiment.class.getName()).log(Level.SEVERE, null, ex);
        }
   
    }//GEN-LAST:event_jButtonChatActionPerformed

    private void jButtonVolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVolverActionPerformed

   try {
/*meto cliente en un arraylist...los eventos tienen arraylist, quizas seria bastante un string*/
            ArrayList<String> alias = new ArrayList();
            alias.add(getClient().getNom());


             /*envio el canvio a mi mismo con boolean de respuesta a false*/
            getClient().onMessage(new EventChangePanel(null,A_PANELINICI,false,alias,getPontPanel()));

   /*envio por el puente de la partida a todos los players de susodicha menos yo,
   * enviando mi nombre para que sepan a quien NO debe enviarselo*/
            this.getPontPanel().send(new EventChangePanel(null,A_PANELMENUWAIT,false,alias,getPontPanel()));
        } catch (RemoteException ex) {
            Logger.getLogger(JPanelCompetition2.class.getName()).log(Level.SEVERE, null, ex);
        }


}//GEN-LAST:event_jButtonVolverActionPerformed

    private void jButtonEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEnviarActionPerformed
       HashMap<String,List<String>>res= new HashMap();
        ArrayList<String> resArrayCombo =  new ArrayList();
       resArrayCombo.add(this.jComboBoxKindOffers.getSelectedItem().toString());

       ArrayList<String> resArray =  new ArrayList();


       resArray.add( getClient().getNom());

       
       res.put(OPCIO_BASICA,resArrayCombo);

        try {
            this.getPontPanel().send(new GameEventInteracctionTwoPanels(-1,resArray, res, this.getPontPanel()));
        } catch (RemoteException ex) {
            Logger.getLogger(JpanelOferiment.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButtonEnviarActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonChat;
    private javax.swing.JButton jButtonEnviar;
    private javax.swing.JButton jButtonVolver;
    private javax.swing.JComboBox jComboBoxKindOffers;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

     public void update(Map<String, List<String>> opcions,ArrayList<String> extras) {

         if(opcions.get(OPCIO_BASICA)!=null){
       this.jComboBoxKindOffers.setModel(new DefaultComboBoxModel(opcions.get(OPCIO_BASICA).toArray()));
         }else if(opcions.get(CHAT)!=null){
        jTextArea1.setText(jTextArea1.getText() + opcions.get(CHAT).toString() + "\n");
//        jTextArea1.setText("          intro??? ");
        }else if(opcions.get(RESPOSTA_PROPOSICIO)!=null){
        if(opcions.get(RESPOSTA_PROPOSICIO).toString().contains(OK)){

             ArrayList<String> alias = new ArrayList();
            alias.add(getClient().getNom());
                try {
                    
                    getClient().onMessage(new EventChangePanel(null,jComboBoxKindOffers.getSelectedIndex(), false, alias, getPontPanel()));
                       this.getPontPanel().send(new EventChangePanel(null,jComboBoxKindOffers.getSelectedIndex()+6, false, alias, getPontPanel()));
                    
                } catch (RemoteException ex) {
                    Logger.getLogger(JpanelOferiment.class.getName()).log(Level.SEVERE, null, ex);
                }

                }
            }
     }

}
